package com.limu.boot;

import ch.qos.logback.core.db.DBHelper;
import com.limu.boot.bean.Pet;
import com.limu.boot.bean.User;
import com.limu.boot.config.MyConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcProperties;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/*
    主程序类
* @SpringBootApplication：这是一个springboot应用
* */
@SpringBootApplication(scanBasePackages = "com.limu")
public class MainApplication {

    public static void main(String[] args) {
        //1.返回我们IOC容器
        ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args);

        //2.查看容器里面的组件
     String[] names = run.getBeanDefinitionNames();
        for (String name : names) {
            System.out.println(name);
        }
        String[] beanNamesForType = run.getBeanNamesForType(WebMvcConfigurer.class);
        System.out.println("====="+beanNamesForType.length);

        //3.从容器中获取组件
        String[] beanNamesForType1 = run.getBeanNamesForType(WebMvcProperties.class);
        System.out.println("======"+beanNamesForType1.length);

        /*   Pet tom01 = run.getBean("tom", Pet.class);

        Pet tom02 = run.getBean("tom", Pet.class);
        System.out.println("组件："+(tom01 == tom02));


        MyConfig bean = run.getBean(MyConfig.class);
        System.out.println(bean);

        //如果@Configuration(proxyBeanMethods = true)代理对象调用方法 SpringBoot总会检查这个组件是否
        //保持组件单例
        User user = bean.user01();
        User user1 = bean.user01();
        System.out.println(user == user1);

        User user01 = run.getBean("user01", User.class);
        Pet tom = run.getBean("tom", Pet.class);

        System.out.println("用户的宠物："+(user01.getPet() == tom));

        //5.获取组件
        String[] names1 = run.getBeanNamesForType(User.class);
        for (String s : names1) {
            System.out.println(s);
        }
        DBHelper bean1 = run.getBean(DBHelper.class);
        System.out.println(bean1);*/

        boolean tom = run.containsBean("user01");
        System.out.println(tom);

        boolean haha = run.containsBean("haha");
        boolean hehe = run.containsBean("hehe");
        System.out.println("haha"+haha+"==hehe"+hehe);
    }
}
